<template>
  <div class="privilege">
    <!-- 按钮 -->
    <div class="btns">
      <el-button type="primary" size="small" @click="toAdd">添加</el-button>
    </div>
    <br>
    <!-- 表格 -->
    <el-table :data="privileges" row-key="id" size="small" :tree-props="{children: 'child'}">
      <el-table-column prop="id" label="序号" width="100"></el-table-column>
      <el-table-column prop="name" label="权限名称" width="120"></el-table-column>
      <el-table-column prop="path" label="路径"></el-table-column>
      <el-table-column prop="type" label="类型"></el-table-column>
      <el-table-column label="图标">
        <template v-slot="scope">
          <img
            style="width:50px; height:50px; border-radius:3px"
            :src="'http://121.199.29.84:8888/'+scope.row.icon"
            alt
          />
        </template>
      </el-table-column>
      <el-table-column prop="hidden" label="隐藏"></el-table-column>
      <el-table-column fixed="right" label="操作" width="160" align="center">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row.id)" size="mini">删除</el-button>
          <el-button type="primary" @click="edit(scope.row)" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框 -->
    <el-dialog title="权限信息" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" clearable placeholder="请输入权限名称"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="path">
          <el-input v-model="form.path" clearable placeholder="请输入路径"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" clearable placeholder="请输入类型"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" clearable placeholder="上传图标后生成路径" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="隐藏" prop="hidden">
          <el-input v-model="form.hidden" clearable placeholder="请输入隐藏"></el-input>
        </el-form-item>
        <el-form-item label="所属权限" prop="parentId">
          <el-select v-model="form.parentId" placeholder="请选择所属权限" clearable="true">
            <el-option v-for="c in privileges" :key="c.id" :label="c.name" :value="c.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图标">
          <el-upload
            class="upload-demo"
            :multiple="false"
            :limit="1"
            :on-success="uploadSuccessHandler"
            :on-error="uploadErrorHandler"
            action="http://121.199.29.84:5588/file/upload"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="close('ruleForm')">取消</el-button>
        <el-button size="small" type="primary" @click="submit('ruleForm')">确定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      name: "权限管理",
      visible: false,
      form: {},
      privileges: [],
      ruleForm: {
        name: "",
        path: "",
        type: "",
        icon: "",
        hidden: "",
        parentId: ""
      },
      rules: {
        name: [{ required: true, message: "请输入权限名称", trigger: "blur" }],
        path: [{ required: true, message: "请输入路径", trigger: "blur" }]
      }
    };
  },
  created() {
    this.loadPrivileges();
  },
  methods: {
    uploadSuccessHandler(response) {
      //console.log(response);
      let icon = response.data.groupname + "/" + response.data.id;
      //this.form.icon = icon;
      this.form = { ...this.form, icon };
    },
    uploadErrorHandler(error) {
      this.$message({ type: "error", message: "上传失败" });
    },
    // 加载权限信息
    loadPrivileges() {
      let url = "http://localhost:8888/privilege/find_all_with_child";
      get(url).then(response => {
        this.privileges = response.data;
      });
    },
    toAdd() {
      this.form = {};
      this.visible = true;
    },
    del(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // 删除
        let url = "http://localhost:8888/privilege/del_by_id";
        get(url, { id: id }).then(response => {
          // 提示
          this.$message({ type: "success", message: response.message });
          // 刷新数据
          this.loadPrivileges();
        });
      });
    },
    edit(row) {
      this.visible = true;
      this.form = row;
    },
    close(form) {
      this.visible = false;
      this.$refs[form].resetFields();
    },
    submit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          // 表单验证成功的情况
          let url = "http://localhost:8888/privilege/save_or_edit";
          post(url, this.form).then(response => {
            this.$message({ type: "success", message: response.message });
            this.visible = false;
            this.loadPrivileges();
            this.$refs[form].resetFields();
          });
          this.loadPrivileges();
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>
.btns {
  margin-bottom: 0.5em;
}
</style>